import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import oracle.jms.AQjmsFactory;
import oracle.jms.AQjmsSession;


public class AQJmsTest {

	public static void main(String[] args) throws Exception {

		test();
		//testSql();

	}
	
	public static void testSql() throws Exception {
		
		Class.forName("oracle.jdbc.driver.OracleDriver");
		
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.24.40.132:1521:webdev", "bendev", "bendev");
		Statement stmt = conn.createStatement();
		
		ResultSet rs = stmt.executeQuery("select * from dbo.user_preference");
		
		while (rs.next()) {
			System.out.println("got row");
		}
		rs.close();
		
		stmt.close();
		conn.close();		
	}

	public static void test() {
		
		try {
			// get queue connection factory
			QueueConnectionFactory qcf = AQjmsFactory.getQueueConnectionFactory("172.24.40.132", "webdev", 1521, "thin");
			
			// create queue connection
			QueueConnection qConn = qcf.createQueueConnection("imaging", "imaging");
			
			// create queue session
			QueueSession qSession = qConn.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
			
			// start the queue connection
			qConn.start();
			
			// get a handle to imaging.IMAGING_QUEUE
			Queue queue = ((AQjmsSession) qSession).getQueue("imaging", "IMAGING_QUEUE");
			
			TextMessage textMessage = qSession.createTextMessage();
			textMessage.setText("text message 1");

			QueueSender sender = qSession.createSender(queue);
			sender.send(textMessage);
			
			qSession.commit();		
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static QueueConnectionFactory getQueueConnectionFactory() throws JMSException {

		Properties props = new Properties();
		props.put("imaging", "imaging");

		return AQjmsFactory.getQueueConnectionFactory("jdbc:oracle:thin:@172.24.40.132:1521:webdev", props);
	}
}
